package com.pwc.conversionTool.rest;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.pwc.conversionTool.config.impl.XMLConfigImpl;
import com.pwc.conversionTool.csvProcess.CsvProcess;


@Controller
public class CsvRest {
	
	
	private static final Logger logger = LoggerFactory.getLogger(CsvRest.class);
	

	@Autowired
	public ServletContext context;
	
	CsvProcess process;
	
	@RequestMapping(value = "/rest/csv/{fileName}", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody List<List<String>> csvContent(String fileName) {
		
		process=new CsvProcess("path");
		
		return process.getTablecontent();
	}
	
	
	
	 @RequestMapping(value = "/csv/uploadFile", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
	 public @ResponseBody String uploadFileHandler( @RequestParam("fileName") String[] names,
	            @RequestParam("file") MultipartFile[] files) 
	 {
		 if (files.length != names.length)
	            return "Mandatory information missing";
	 
	        String message = "";
	        for (int i = 0; i < files.length; i++) {
	            MultipartFile file = files[i];
	            String fileName = names[i];
		  
	            if (!file.isEmpty()) {
		           try {
		        	   File folderFile=new File(XMLConfigImpl.initial(context).getUploadPath());
		        	   
		               byte[] bytes = file.getBytes();
		               
		               String FileName=fileName.equals("")?file.getOriginalFilename():fileName;
		               
		               String path=folderFile.getPath()+File.separator+FileName;
		               
		               File saveFile=new File(path);
		               
		               if(!saveFile.exists())
		               {
		            	   FileUtils.writeByteArrayToFile(saveFile, bytes);
		               }else {
		            	   saveFile=new File(context.getContextPath()+File.separator+System.currentTimeMillis()+FileName);
		            	   FileUtils.writeByteArrayToFile(saveFile, bytes);
					}
		               
		               message= "upload success";
		           } catch (Exception e) {
		        	   message= " failed to upload "  + e.getMessage();
		           }
		       } 
		       else {
		    	   return " failed to upload , because the file was empty.";
		       }
	        }
	       return message;
	       
	   }

	

}
